# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.1.6
pkgrel=1
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
license=('GPL')
groups=('base')
makedepends=('cocom'
             'texinfo'
             'git'
             'perl'
             'gcc'
             'make'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'diffutils')
# options=('debug' '!strip')
source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-${pkgver//./_}-release
        0001-Add-MSYS2-triplet.patch
        0002-Rename-dll-from-cygwin-to-msys.patch
        0003-Add-functionality-for-converting-UNIX-paths-in-argum.patch
        0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
        0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
        0006-Do-not-create-cygwin-symlinks.-Instead-use-deep-copy.patch
        0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch
        0008-Do-not-convert-environment-for-strace.patch
        0009-Special-case-for-converting-root-directory-to-have-t.patch
        0010-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0011-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0012-strace.cc-Don-t-set-MSYS-noglob.patch
        0013-Add-debugging-for-build_argv.patch
        0014-Add-debugging-for-strace-make_command_line.patch
        0015-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0016-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
        0017-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch
        0018-strace-quiet-be-really-quiet.patch
        0019-Default-to-disable_pcon.patch
        0020-Introduce-the-enable_pcon-value-for-MSYS.patch
        0021-Don-t-create-WSL-symlinks-for-MSYS2.patch
        0022-Fix-incorrect-path-conversion-trailing-slash.patch
        0023-Pass-the-TZ-variable-to-non-msys-programs-if-it-is-s.patch
        0024-Pass-environment-variables-with-empty-values.patch
        0025-Handle-8-bit-characters-under-LOCALE-C.patch
        0026-Mention-the-extremely-useful-small_printf-function.patch
        0027-Add-a-helpful-debug-message-for-posix-to-windows-con.patch
        0028-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch
        0029-Avoid-unnecessary-recursion-in-find_path_start_and_t.patch
        0030-Leave-arguments-starting-with-a-tilde-or-quote-alone.patch
        0031-Leave-Git-s-name-and-message-arguments-alone-please.patch
        0032-Leave-paths-containing-any-special-characters-alone.patch
        0033-Leave-paths-containing-alone.patch
        0034-Skip-posix-to-windows-conversion-when-is-seen.patch
        0035-Also-leave-Git-s-rev-.-name-syntax-alone.patch
        0036-Arguments-starting-with-are-no-paths.patch
        0037-Prevent-scp-style-arguments-from-being-mangled.patch
        0038-Fixed-path-converting-with-non-ascii-char.patch
        0039-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0040-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0041-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0042-kill-kill-Win32-processes-more-gently.patch
        0043-Allow-overriding-the-home-directory-via-the-HOME-var.patch
        0044-Respect-db_home-setting-even-for-the-SYSTEM-account.patch
        0045-Respect-the-db_home-env-setting-under-more-circumsta.patch
        0046-Allow-native-symlinks-to-non-existing-targets-in-nat.patch
        0047-POSIX-ify-the-SHELL-variable.patch
        0048-Make-paths-WCS-MBS-conversion-explicit.patch
        0049-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0050-Use-MB_CUR_MAX-6-by-default.patch
        0051-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0052-Mirror-master-and-main.patch)
sha256sums=('SKIP'
            'd744a049e20f8201dc662164beb8fb0c1853dd2624a1bc6f4b016fd96e82c737'
            '0498b79408cbfa53f826df34f98e86c923ab620f44dcd5f04193e512e75eb9d3'
            '308b57cdb84976629ef5e6fa2c7c6da09496267ffbc4af28db38ee38f7e640c8'
            'b97fc9f9e03dafc7d9093c8332511c00419ae6d5c240f26bebd6dcd9f1b31e1d'
            '95496a41681115837996c801bf8d6fe1b230315e10d225645d402047f4c44e41'
            'a9379fbe45cc8b23b9d8ce640dd60eb5037b3084a282cb9cf4747bdf2abc5828'
            '5edd323f432e56e6ef3be97e7f12d486f0269ba5b51f3b347564a05e4b522ac9'
            '69459c0bc92f10abab681e6aa1e17a95648973ebe95a8ecaf4b98d2b68cb839a'
            '784a2188d0e4954ba09d6af7fb670b003b24b7811418f84c006c7b0b7a570ae5'
            '523402c0d98a216a3feed2e480e1c5ec609b920b59a70e82c481124c4446b183'
            '29ce2074555ac1f3695310c2f6b31241aba1af98764d9bf8e4db4ce445186eb8'
            'fc61c4646348817310ce6184be33022a02500c1b027a62b1e2d7c60af245e7c8'
            '48ad83fcd9188183de83b2f287ceeb713402dd1cd2632bb9db93c6d3d1e85bd0'
            '2eed8941b0c895c9f65c2c13a086b0b9623afa937f9beab7dbf37d327742fece'
            '87168f58a203aec06895f4f2ab168927676b33516875b6d2b6d3506145cb9c56'
            'ba4b6e2d9d48661ed110b9cb1a985eb9af3e29cd90dadf041bb054321ab72e06'
            '841fe825c4b45cf58c49589619628a91d51a26acfca8619e07a156d397f109f5'
            '49d6df30c4c9a500327eaf3cf8d326100d695e63df3630e3862faff5ef4edd28'
            '1e3a1274f4f0ad2b40b4ee505ee47c7c336af37c80a39ff19318dc68d842d995'
            '227db4e3ebff3b20305970cc2aef141de5b2233f6b5abf38e0d13d4f9bf8833a'
            '27de372c0460cae269c78515101a022dad6a9b3a307c8f5e306b5e9634cf635b'
            '8ae345dd3cf309f494ee8548467930fc753625629167c83dabbf54b25806fce4'
            'c92d6b5f68f1d5ab5d1bbd7066e079342c8e94567c611b060505c31e2c79ca19'
            '4d66455e4ab845bd8cc095e8c1932d0f605b1304a374046f22c4a233ad80e5b6'
            '04a0141b10cf7cce8cbd5495c2224a5fbf00582376c101396687cd5049a4f659'
            'd766d0e25b56405e4eb9adf6557f328113a457b20fc9fa47a8c056bf5a912ae8'
            '07a89cebf447749fe1fdb0c92026461c990a17a3d0d5b09d16f098d97c0eb4a4'
            'db3c66f2a41ac9f47dfed3372f6f44565589c4af30869e6b5b5a693e400cd9bc'
            '7568d9068217ec216aac69af84d8fa4791cd8bec5afede0c3c19e2d8fcf30550'
            '611412d272b0ad617e434b683bd573cbf1cb98b627fe702584fb61d70974b4ba'
            '01bbab26b81b516f92047c7d9996d0dc00933f6b864b7ac1f9f92ac55ed18c7f'
            'ddbf754cd45558521158df4116f157be332f1ae06073fa06e2a559e22714c74b'
            '0a58c71bf90022594674aa0931fa0401171ba7bbcacf2f8b32ab65a4a16c3d24'
            '7fd0c24ecdf536257b484b7e1b33d4ec836c6a26c4060d862e0c2fdb637a394b'
            '074fd60d44d55dbdde7a6854f3a7fe626b1d06d0d16a25c50d8b6310072a3f3f'
            'b5612aab0ebbc251699ec692b94397968627f8bfaad4d92e53e1b3f713a1d34b'
            'e8a4e1d13a75ddd7d26427d1862da6645dad0edcf47091da1b15fc3161a0298f'
            '00fe2d0f65ad0e5806d02738e57923e7d20076f98f05db45cdbb47b638bbf480'
            '6ac1457eb29eea97fd9ef6f09e6212ae4dea59d50e03a79a548bebaccc61c3b3'
            '1f03ce4081b37f22c30402f7010f121c7fd05704003ebd8a81611b9b9811e247'
            'ecc7b86e620800a731a1d49cfb8fee21f5a9ad36f5751303decc7a226dbd9aec'
            'c5de4c3e44d94ae5ab188690f66dc9427739cf30be4abb0bd80b2320a0ba7529'
            'dabc15dcfc406a2ff6acf8ffc75f26b23d6a219d1cf2a8b9a8e5f6936037db54'
            'af88f73a5f42e40bced510bd62ac1b6a41d3a8e5c1b7d330eb86b87fc7858003'
            '973cedd99691c2fdd24da1a4f2119b0f4da8368acc9d8401f3429983534add77'
            '7ff49b2df4de1a79cd1e1a51b3a5a99d93d8fa1ef8c4d82c2c8c346bbe155da0'
            '7c4198309729dd296b5c2a587216cce3a18dd6960efb538a3590cd18a6cfe72c'
            'f767e92fef5387bc725b5f01dfbd4612c870e5b017082600da3bcbc925cafbef'
            '90ae7bcaf58c492b869b3ff9083905efb8aa704b9e83c1e772487980414bae0f'
            '1f27162ed8a84d682d36776445ce745b54be8be53683811c4ee9bb5ef152863d'
            '8f701a14aff501fc053f6a62e05c6ef34e7b0b936ccb6b8df54e2d35085e668b'
            'b7006e11f625e47ae19cfce1ff2433175afcebb577b324493e6e9bbf25927326')

# Helper macro to help make tasks easier #
del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}

prepare() {
  cd "${srcdir}"/msys2-runtime
  if test true != "$(git config core.symlinks)"
  then
    git config core.symlinks true &&
    /usr/bin/git reset --hard
  fi
  del_file_exists winsup/cygwin/msys2_path_conv.cc \
    winsup/cygwin/msys2_path_conv.h
  git am --committer-date-is-author-date "${srcdir}"/0001-Add-MSYS2-triplet.patch
  git am --committer-date-is-author-date "${srcdir}"/0002-Rename-dll-from-cygwin-to-msys.patch
  git am --committer-date-is-author-date "${srcdir}"/0003-Add-functionality-for-converting-UNIX-paths-in-argum.patch
  git am --committer-date-is-author-date "${srcdir}"/0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
  git am --committer-date-is-author-date "${srcdir}"/0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
  git am --committer-date-is-author-date "${srcdir}"/0006-Do-not-create-cygwin-symlinks.-Instead-use-deep-copy.patch
  git am --committer-date-is-author-date "${srcdir}"/0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin-With-.patch
  git am --committer-date-is-author-date "${srcdir}"/0008-Do-not-convert-environment-for-strace.patch
  git am --committer-date-is-author-date "${srcdir}"/0009-Special-case-for-converting-root-directory-to-have-t.patch
  git am --committer-date-is-author-date "${srcdir}"/0010-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
  git am --committer-date-is-author-date "${srcdir}"/0011-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
  git am --committer-date-is-author-date "${srcdir}"/0012-strace.cc-Don-t-set-MSYS-noglob.patch
  git am --committer-date-is-author-date "${srcdir}"/0013-Add-debugging-for-build_argv.patch
  git am --committer-date-is-author-date "${srcdir}"/0014-Add-debugging-for-strace-make_command_line.patch
  git am --committer-date-is-author-date "${srcdir}"/0015-environ.cc-New-facility-environment-variable-MSYS2_E.patch
  git am --committer-date-is-author-date "${srcdir}"/0016-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
  git am --committer-date-is-author-date "${srcdir}"/0017-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch
  git am --committer-date-is-author-date "${srcdir}"/0018-strace-quiet-be-really-quiet.patch
  git am --committer-date-is-author-date "${srcdir}"/0019-Default-to-disable_pcon.patch
  git am --committer-date-is-author-date "${srcdir}"/0020-Introduce-the-enable_pcon-value-for-MSYS.patch
  git am --committer-date-is-author-date "${srcdir}"/0021-Don-t-create-WSL-symlinks-for-MSYS2.patch
  git am --committer-date-is-author-date "${srcdir}"/0022-Fix-incorrect-path-conversion-trailing-slash.patch
  git am --committer-date-is-author-date "${srcdir}"/0023-Pass-the-TZ-variable-to-non-msys-programs-if-it-is-s.patch
  git am --committer-date-is-author-date "${srcdir}"/0024-Pass-environment-variables-with-empty-values.patch
  git am --committer-date-is-author-date "${srcdir}"/0025-Handle-8-bit-characters-under-LOCALE-C.patch
  git am --committer-date-is-author-date "${srcdir}"/0026-Mention-the-extremely-useful-small_printf-function.patch
  git am --committer-date-is-author-date "${srcdir}"/0027-Add-a-helpful-debug-message-for-posix-to-windows-con.patch
  git am --committer-date-is-author-date "${srcdir}"/0028-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch
  git am --committer-date-is-author-date "${srcdir}"/0029-Avoid-unnecessary-recursion-in-find_path_start_and_t.patch
  git am --committer-date-is-author-date "${srcdir}"/0030-Leave-arguments-starting-with-a-tilde-or-quote-alone.patch
  git am --committer-date-is-author-date "${srcdir}"/0031-Leave-Git-s-name-and-message-arguments-alone-please.patch
  git am --committer-date-is-author-date "${srcdir}"/0032-Leave-paths-containing-any-special-characters-alone.patch
  git am --committer-date-is-author-date "${srcdir}"/0033-Leave-paths-containing-alone.patch
  git am --committer-date-is-author-date "${srcdir}"/0034-Skip-posix-to-windows-conversion-when-is-seen.patch
  git am --committer-date-is-author-date "${srcdir}"/0035-Also-leave-Git-s-rev-.-name-syntax-alone.patch
  git am --committer-date-is-author-date "${srcdir}"/0036-Arguments-starting-with-are-no-paths.patch
  git am --committer-date-is-author-date "${srcdir}"/0037-Prevent-scp-style-arguments-from-being-mangled.patch
  git am --committer-date-is-author-date "${srcdir}"/0038-Fixed-path-converting-with-non-ascii-char.patch
  git am --committer-date-is-author-date "${srcdir}"/0039-path-conversion-Introduce-ability-to-switch-off-conv.patch
  git am --committer-date-is-author-date "${srcdir}"/0040-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
  git am --committer-date-is-author-date "${srcdir}"/0041-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
  git am --committer-date-is-author-date "${srcdir}"/0042-kill-kill-Win32-processes-more-gently.patch
  git am --committer-date-is-author-date "${srcdir}"/0043-Allow-overriding-the-home-directory-via-the-HOME-var.patch
  git am --committer-date-is-author-date "${srcdir}"/0044-Respect-db_home-setting-even-for-the-SYSTEM-account.patch
  git am --committer-date-is-author-date "${srcdir}"/0045-Respect-the-db_home-env-setting-under-more-circumsta.patch
  git am --committer-date-is-author-date "${srcdir}"/0046-Allow-native-symlinks-to-non-existing-targets-in-nat.patch
  git am --committer-date-is-author-date "${srcdir}"/0047-POSIX-ify-the-SHELL-variable.patch
  git am --committer-date-is-author-date "${srcdir}"/0048-Make-paths-WCS-MBS-conversion-explicit.patch
  git am --committer-date-is-author-date "${srcdir}"/0049-Handle-ORIGINAL_PATH-just-like-PATH.patch
  git am --committer-date-is-author-date "${srcdir}"/0050-Use-MB_CUR_MAX-6-by-default.patch
  git am --committer-date-is-author-date "${srcdir}"/0051-popen-call-usr-bin-sh-instead-of-bin-sh.patch
  git am --committer-date-is-author-date "${srcdir}"/0052-Mirror-master-and-main.patch
}

build() {
  [[ -d "${srcdir}"/build-${CHOST} ]] && rm -rf "${srcdir}"/build-${CHOST}
  mkdir -p "${srcdir}"/build-${CHOST} && cd "${srcdir}"/build-${CHOST}

  # Gives more verbose compile output when debugging.
  local -a extra_config
  if check_option "debug" "y"; then
    export CCWRAP_VERBOSE=1
    OPTIM="-O0"
    extra_config+=(--enable-debugging)
  else
    OPTIM="-O2"
  fi

  CFLAGS="$OPTIM -pipe -ggdb"
  CXXFLAGS="$OPTIM -pipe -ggdb"

  "${srcdir}"/msys2-runtime/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  LC_ALL=C make
  LC_ALL=C make -j1 DESTDIR="${srcdir}"/dest install

  #pushd ${CHOST}/winsup/cygwin > /dev/null
  #LANG=C make libmsys2_s.a
  #cp libmsys2_s.a "${srcdir}"/dest/usr/${CHOST}/lib/
  #popd > /dev/null

  rm -rf "${srcdir}"/dest/etc
}

package_msys2-runtime() {
  pkgdesc="Posix emulation engine for Windows"
  groups=('base')
  options=('!strip')
  conflicts=('catgets' 'libcatgets')
  replaces=('catgets' 'libcatgets')
  #install=msys2-runtime.install

  mkdir -p "${pkgdir}"/usr
  cp -rf "${srcdir}"/dest/usr/bin "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/bin/msys-2.0.dbg
  rm -f "${pkgdir}"/usr/bin/cyglsa-config
  rm -f "${pkgdir}"/usr/bin/cyglsa.dll
  rm -f "${pkgdir}"/usr/bin/cyglsa64.dll
  rm -f "${pkgdir}"/usr/bin/cygserver-config
  cp -rf "${srcdir}"/dest/usr/libexec "${pkgdir}"/usr/
  cp -rf "${srcdir}"/dest/usr/share "${pkgdir}"/usr/
}

package_msys2-runtime-devel() {
  pkgdesc="MSYS2 headers and libraries"
  groups=('msys2-devel')
  depends=("msys2-runtime=${pkgver}")
  options=('staticlibs' '!strip')
  conflicts=('libcatgets-devel')
  replaces=('libcatgets-devel')

  mkdir -p "${pkgdir}"/usr/bin
  cp -f "${srcdir}"/dest/usr/bin/msys-2.0.dbg "${pkgdir}"/usr/bin/
  cp -rLf "${srcdir}"/dest/usr/${CHOST}/include "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/include/iconv.h
  rm -f "${pkgdir}"/usr/include/unctrl.h
  # provided by libtirpc
  rm -fr "${pkgdir}"/usr/include/rpc/

  cp -rLf "${srcdir}"/dest/usr/${CHOST}/lib "${pkgdir}"/usr/
}

# return 0
# To hack on this:
# cd /c/repo-MSYS2/msys2-runtime/
# pushd src/build-i686-pc-msys
# LANG=C make && make -j1 DESTDIR=/c/repo-MSYS2/msys2-runtime/src/dest install
# popd
# makepkg -sRLf
# pacman -U msys2-runtime*.xz

# Quicker:
# open cmd.exe
# set "PATH=C:\\msys32\\usr\\bin;%PATH%"
# E:
# pushd E:\m2\repo-MSYS2\msys2-runtime\src\build-i686-pc-msys\i686-pc-msys\winsup\cygwin
# C:/msys32/usr/bin/bash -c "LANG=C && make"
# copy /y new-msys-2.0.dll C:\msys32\usr\bin\msys-2.0.dll
# popd
# C:
# C:/msys32/usr/bin/strace ls / > C:/strace.txt 2>&1
#
